﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;

namespace SecurityCheck
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            timer1.Interval = 1000;
            // 启用定时器
            timer1.Enabled = true;
            // 关联Tick事件
            timer1.Tick += timer1_Tick;
        }
        private string connectionString = "Server=127.0.0.1\\SQLEXPRESS,1433;Database=test_empty;User Id=sa;Password=bytq@123;";
        private string tableName = "TestConditionResult";
        private string tableName1 = "TestInfoBaseSerialNo";

        private void label17_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void timer1_Tick(object sender, EventArgs e)
        {


            XunhuanReadWareHouse();
            //XunhuanReadProduct();
            //Report();


        }
        public List<string> ReadMultipleColumnsToArrays()
        {
            //string connectionString = "Server=.;Database=TestDB;Integrated Security=true;";
            //string query = $"SELECT WareHouseNo,ProductType,Quality,Isable,IsFull,UpdateTime FROM {tableName}";
            //SELECT TOP 10 SerialNo FROM TestConditionResult WHERE SerialNo IS NOT NULL GROUP BY SerialNo ORDER BY MAX(SystemTime) DESC;
            //string query = $"SELECT TOP 10 SerialNo FROM {tableName} WHERE SerialNo IS NOT NULL GROUP BY SerialNo ORDER BY MAX(SystemTime) DESC";
            string query = $"SELECT TOP 10 SerialNo FROM {tableName1} WHERE SerialNo IS NOT NULL GROUP BY SerialNo ORDER BY MAX(SystemTime) DESC";

            List<object[]> dataList = new List<object[]>();
            List<string> dataListstr = new List<string>();

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    connection.Open();
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        int columnCount = reader.FieldCount;

                        while (reader.Read())
                        {
                            object[] rowData = new object[columnCount];
                            for (int i = 0; i < columnCount; i++)
                            {
                                rowData[i] = reader.IsDBNull(i) ? null : reader.GetValue(i);
                            }
                            dataList.Add(rowData);
                            dataListstr.Add(string.Join("; ", rowData));
                        }
                    }
                }
            }

            // 转换为二维数组
            object[][] dataArray = dataList.ToArray();

            // 输出结果
            for (int i = 0; i < dataArray.Length; i++)
            {
                Console.WriteLine($"行 {i + 1}: {string.Join(", ", dataArray[i])}");
            }
            return dataListstr;
        }
        public List<Tuple<string, string, float>> ReadResult(List<string> dataShow)
        {
            List<Tuple<string, string, float>> resultList = new List<Tuple<string, string, float>>();
            //string connectionString = "Server=.;Database=TestDB;Integrated Security=true;";
            //string query = $"SELECT WareHouseNo,ProductType,Quality,Isable,IsFull,UpdateTime FROM {tableName}";
            //SELECT TOP 10 SerialNo FROM TestConditionResult WHERE SerialNo IS NOT NULL GROUP BY SerialNo ORDER BY MAX(SystemTime) DESC;
            foreach (var data in dataShow)
            {



                string query = $"SELECT TIName, ResultValue FROM {tableName} where SerialNo = '{data}'";




                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    using (SqlCommand command = new SqlCommand(query, connection))
                    {
                        connection.Open();
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                string tiName = reader["TIName"].ToString();
                                float resultValue = Convert.ToSingle(reader["ResultValue"]);

                                resultList.Add(Tuple.Create(data, tiName, resultValue));
                            }
                        }
                    }
                }
            }



            return resultList;




            // 转换为二维数组

        }
        public List<Tuple<string, string>> ReadSecurityResult(List<string> dataShow)
        {
            List<Tuple<string, string>> resultList = new List<Tuple<string, string>>();
            //string connectionString = "Server=.;Database=TestDB;Integrated Security=true;";
            //string query = $"SELECT WareHouseNo,ProductType,Quality,Isable,IsFull,UpdateTime FROM {tableName}";
            //SELECT TOP 10 SerialNo FROM TestConditionResult WHERE SerialNo IS NOT NULL GROUP BY SerialNo ORDER BY MAX(SystemTime) DESC;
            foreach (var data in dataShow)
            {



                string query = $"SELECT SerialNo, TestResult FROM {tableName1} where SerialNo = '{data}'";




                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    using (SqlCommand command = new SqlCommand(query, connection))
                    {
                        connection.Open();
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                string tiName = reader["SerialNo"].ToString();
                                //float resultValue = Convert.ToSingle(reader["TestResult"]);
                                string resultValue = reader["TestResult"].ToString();
                                resultList.Add(Tuple.Create(tiName, resultValue));
                            }
                        }
                    }
                }
            }



            return resultList;




            // 转换为二维数组

        }
        public void XunhuanReadWareHouse()
        {
            List<string> ware001 = ReadMultipleColumnsToArrays();
            var ware001pare = ware001[0];
            var ware002pare = ware001[1];
            var ware003pare = ware001[2];
            var ware004pare = ware001[3];
           // var result = ReadResult(ware001);
            var resultSecurity = ReadSecurityResult(ware001);
            for (int i = 0; i < 10; i++)
            {
                string controlName = "textBox" + (i + 1);
                TextBox textBox = this.Controls.Find(controlName, true).FirstOrDefault() as TextBox;
                if (textBox != null)
                {

                    textBox.Text = resultSecurity[i].Item1.ToString();

                }
            }
            for (int i = 0; i < 10; i++)
            {
                string controlName = "textBox" + (i + 11);
                TextBox textBox = this.Controls.Find(controlName, true).FirstOrDefault() as TextBox;
                if (textBox != null)
                {
                    
                    textBox.Text = resultSecurity[i ].Item2.ToString();
                    if (textBox.Text == "FAIL")
                    {
                        textBox.Text = "失败";
                            textBox.BackColor = Color.Red;

                    }
                    if (textBox.Text == "PASS")
                    {
                        textBox.Text = "成功";
                        textBox.BackColor = Color.Green;

                    }

                }
            }
           
                }
            }
            
           

        }

    


   